<template>
  <el-dialog v-model="open" :title="title" width="800px" append-to-body :close-on-click-modal="false">
    <el-form ref="dialogRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="会员姓名">
        <span>{{ form.memberName }}</span>
      </el-form-item>
      <el-form-item label="延期类型" prop="expireDateType">
        <el-select v-model="form.expireDateType">
          <el-option label="按时间" value="0" />
          <el-option label="按日期" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item v-show="form.expireDateType=='0'" label="延期时间" prop="expireDate0">
        <el-select v-model="form.expireDateChangeType">
          <el-option label="天" value="0" />
          <el-option label="月" value="1" />
          <el-option label="年" value="2" />
        </el-select>
        <el-input v-model="form.expireDateChangeValue" type="number">
          <template #append>{{ form.validityType=='0'?'天':(form.validityType=='1'?'月':'年') }}</template>
        </el-input>
      </el-form-item>
      <el-form-item v-show="form.expireDateType=='1'" label="新到期日期" prop="expireDate1">
        <el-date-picker v-model="form.expireDate" :disabled-date="disabledDate" type="date" placeholder="到期日期" value-format="YYYY-MM-DD" />
      </el-form-item>
      <el-form-item label="续费费用" prop="actualAmount">
        <el-input v-model="form.actualAmount" type="number" />
      </el-form-item>
      <el-form-item label="到账费用" prop="finalAmount">
        <el-input v-model="form.finalAmount" type="number" />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="form.payType" property="支付方式">
          <el-option label="扫码" value="0" />
          <el-option label="现金" value="1" />
          <el-option label="微信" value="2" />
          <el-option label="支付宝" value="3" />
          <el-option label="其他" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="会员等级" prop="cardTypeId">
        <el-select v-model="form.cardTypeId" @change="selectedCard">
          <el-option v-for="card in props.cardList" :key="card.id" :label="card.name" :value="card.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="优惠折扣" prop="discountPercent">
        <el-input v-model="form.discountPercent" type="number" placeholder="优惠折扣">
          <template #append>%</template>
        </el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>

  </el-dialog>
</template>

<script setup>
import { getCurrentInstance } from '@vue/runtime-core'
import { reactive, watch } from 'vue'
import { toRefs } from '@vueuse/shared'
import { memberRenew } from '../../../../../api/ven/member'
import { dayjs } from 'element-plus'

const { proxy } = getCurrentInstance()

const props = defineProps({
  props: {
    type: Object,
    default: () => ({
      open: false,
      title: ''
    })
  },
  cardList: {
    type: Array,
    default: () => []
  }
})
const emit = defineEmits(['submitForm'])

const formBase = {
  expireDateType: '1'
}
const data = reactive({
  form: {
    ...formBase
  },
  rules: {
    expireDateType: [{ required: true }],
    expireDate0: [{
      validator: (rule, value, callback) => {
        if (form.value.expireDateType === '0') {
          if (!form.value.expireDateChangeType || !form.value.expireDateChangeValue) {
            callback('必填')
            return
          }
        }
        callback()
      }, required: true }],
    expireDate1: [{
      validator: (rule, value, callback) => {
        if (form.value.expireDateType === '1') {
          if (!form.value.expireDate) {
            callback('必填')
            return
          }
        }
        callback()
      }, required: true }],
    actualAmount: [{ required: true, message: '必填', trigger: 'blur' }],
    finalAmount: [{ required: true, message: '必填', trigger: 'blur' }],
    payType: [{ required: true, message: '必填', trigger: 'change' }],
    cardTypeId: [{ required: true, message: '必填', trigger: 'change' }],
    discountPercent: [{
      validator: (rule, value, callback) => {
        if (!value) {
          callback('必填')
          return
        } else {
          if (value < 0 || value > 100) {
            callback('输入有误')
            return
          }
        }
        callback()
      }, required: true }]
  }
})
const { open, title } = toRefs(props.props)
const { form, rules } = toRefs(data)

watch(open, (val) => {
  if (val) {
    const { id, expirationTime, memberName, cardTypeId, discountPercent } = props.props.member
    form.value = _.assign({
      memberId: id,
      memberName,
      cardTypeId,
      discountPercent,
      expireDate: expirationTime,
      expirationTime
    }, { ...formBase })
  } else {
    proxy.resetForm('dialogRef')
  }
})

function disabledDate(time) {
  return time.getTime() < dayjs(form.value.expirationTime).valueOf()
}

function selectedCard(id) {
  const card = _.find(props.cardList, (c) => c.id === id)
  form.value.discountPercent = card.discountPercent
}

function submitForm() {
  proxy.$refs['dialogRef'].validate((valid) => {
    if (valid) {
      memberRenew(form.value).then(() => {
        proxy.$modal.msgSuccess('操作成功')
        open.value = false
        emit('submitForm')
      })
    }
  })
}

function cancel() {
  open.value = false
}

</script>

<style scoped>

</style>
